package com.file.system.service.impl.system;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.file.system.entity.res.DataPage;
import com.file.system.entity.sort.BaseSort;
import com.file.system.entity.system.WebMenu;
import com.file.system.mapper.system.WebMenuMapper;
import com.file.system.mapper.system.WebRoleMenuMapper;
import com.file.system.mapper.system.WebRoleNodeMapper;
import com.file.system.mapper.system.WebRoleUserMapper;
import com.file.system.service.system.IWebMenuService;
import com.file.system.tools.page.DP;
import com.file.system.tools.sort.SortUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 菜单 实现
 *
 * @author jch
 * @since 2022-08-05
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WebMenuServiceImpl implements IWebMenuService {

    @Autowired
    private WebMenuMapper mapper;
    @Autowired
    private SortUtil sortUtil;
    @Autowired
    private WebRoleMenuMapper webRoleMenuMapper;
    @Autowired
    private WebRoleUserMapper webRoleUserMapper;
    @Autowired
    private WebRoleNodeMapper webRoleNodeMapper;

    //添加菜单
    public boolean save(WebMenu webMenu) {
        webMenu.setId(IdUtil.getSnowflakeNextId());
        //排序
        webMenu = sortUtil.add(webMenu, webMenu.getClass(), "web_menu",
                BaseSort.eq().val("is_del", "0"),
                BaseSort.eq().val("pid", webMenu.getPid())
        );
        if (ObjectUtil.isNull(webMenu)) {
            return false;
        }
        //设置角色标识
        WebMenu parent = mapper.selectById(webMenu.getPid());
        if (ObjectUtil.isNotNull(parent)) {
            webMenu.setAuth(parent.getAuth() + ":" + webMenu.getAuth());
        }
        return SqlHelper.retBool(mapper.save(webMenu));
    }


    //删除菜单
    public boolean removeById(@Param("id") Long id) {
        WebMenu webMenu = mapper.selectById(id);
        sortUtil.del(webMenu, webMenu.getClass(), "web_menu",
                BaseSort.eq().val("is_del", "0"),
                BaseSort.eq().val("pid", webMenu.getPid())
        );
        //删除关联菜单
        webRoleMenuMapper.removeMenuByRoleId(id);
        //删除关联用户
        webRoleUserMapper.removeUserByRoleId(id);
        //删除关联节点
        webRoleNodeMapper.removeNodeByRoleId(id);

        return SqlHelper.retBool(mapper.removeById(new WebMenu().setId(id)));
    }

    //编辑菜单
    public boolean updateById(@Param("et") WebMenu webMenu) {
        if (ObjectUtil.isEmpty(webMenu.getId())) {
            return false;
        }
        WebMenu webMenuOld = mapper.selectById(webMenu.getId());
        //排序
        webMenu = sortUtil.update(webMenu, webMenu.getClass(), webMenuOld.getSort(), "web_menu",
                BaseSort.eq().val("is_del", "0"),
                BaseSort.eq().val("pid", webMenuOld.getPid())
        );
        if (ObjectUtil.isNull(webMenu)) {
            return false;
        }
        return SqlHelper.retBool(mapper.updateById(webMenu));
    }

    //编辑
    public boolean update(@Param("ew") UpdateWrapper<WebMenu> query) {
        return SqlHelper.retBool(mapper.update(query));
    }

    //添加菜单(批量)
    public boolean saveBatch(@Param("list") List<WebMenu> webMenuList) {
        if (webMenuList.size() < 1) {
            return false;
        }
        return SqlHelper.retBool(mapper.saveBatch(webMenuList));
    }

    //查询菜单
    @DS("slave")
    public List<WebMenu> list(@Param("ew") QueryWrapper<WebMenu> query) {
        return mapper.select(query);
    }

    //通过ids查询菜单集合
    @DS("slave")
    public List<WebMenu> listByIds(List<Long> ids) {
        QueryWrapper<WebMenu> query = new QueryWrapper<>();
        query.in("a.id", ids);
        query.orderByAsc("a.sort");
        return mapper.select(query);
    }

    //查询所有非删除状态菜单
    @DS("slave")
    public List<WebMenu> listAll() {
        QueryWrapper<WebMenu> query = new QueryWrapper<>();
        query.eq("a.is_del", 0);
        query.orderByAsc("a.sort");
        return mapper.select(query);
    }

    //查询菜单分页
    @DS("slave")
    public DataPage page(@Param("pageIndex") Integer pageIndex, @Param("pageSize") Integer pageSize, @Param("ew") QueryWrapper<WebMenu> query) {
        //查询数量
        int count = mapper.selectCount(query);
        DP dp = DP.init().paging(pageIndex, pageSize, count);
        Page<WebMenu> page = new Page<>(dp.getPid(), dp.getPageSize());
        page.setSearchCount(false);
        List<WebMenu> list = mapper.select(page, query);
        return DataPage.init().set(count, list);
    }

    //通过id查询菜单
    @DS("slave")
    public WebMenu getById(@Param("id") Long id) {
        return mapper.selectById(id);
    }

    //通过条件查询菜单
    @DS("slave")
    public WebMenu getOne(@Param("ew") QueryWrapper<WebMenu> query) {
        List<WebMenu> list = mapper.select(query);
        return list.size() > 0 ? list.get(0) : null;
    }

    //查询菜单数量
    @DS("slave")
    public int count(@Param("ew") QueryWrapper<WebMenu> query) {
        return mapper.selectCount(query);
    }
}
